<?php

namespace app\common\cachemodel\zl2;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl2Surprise extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl2_surprise';

    protected $cacheModel = 'astro_fortune.surprise';

    public function getAllIds(int $type, int $secondType = 0)
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $type)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($type, $secondType)
            {
                try
                {
                    $where = ['type' => $type];
                    if ($type === 2)
                    {
                        $where['second_type'] = $secondType;
                    }
                    $where['status'] = 1;
                    $data = $this->where($where)->column('id');
                    $resData = [];
                    foreach ($data as $id)
                    {
                        $resData[$id] = ['id' => $id];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return $resData['data'];
    }

    public function getAllData(int $type, int $secondType = 0)
    {
        $pageData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $type)
            ->setPage(-1)
            ->setListKeyType($secondType)
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($type, $secondType)
            {
                return $this->getInfoByIdsInData($ids, [$type, $secondType], $sortType);
            }, 'id', ['id', 'extend']);

        return $this->dataInit($pageData['data']);
    }

    /**
     * 批量获取 数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $data = $this->where(['id' => ['in', $ids]])->column('cname,status,type,second_type,join_id,extend,create_time', 'id');
            } else
            {
                $where = ['type' => $type[0]];
                if ($type[0] == 2)
                {
                    $where['second_type'] = $type[1];
                }
                $where['status'] = 1;
                $data = $this->where($where)->column('cname,status,type,second_type,join_id,extend,create_time', 'id');
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'status', 'type', 'second_type', 'join_id', 'create_time']);

            if (isset($itemData['extend']))
            {
                $data[$index]['extend'] = json_decode($itemData['extend'], true);
            }
        }

        return $data;
    }
}